package javaNIO1.Selectors;


import oracle.ucp.logging.annotations.Logging;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with Intel IDEA.
 * 提取匹配的表达式
 *
 * @Auther: gjt
 * @Date: 2024/8/3 9:18
 */
public class EmailAddressFinder {
    public static void main(String[] args) {
//        replaceFirst();
        replace(args);

    }


    private static void replaceFirst() {
        String regex = "\\p{Blank}";
        Pattern pattern = Pattern.compile(regex);

        // 测试不同的字符串
        String[] testStrings = {" ", "\t", "Hello World", "Hello\tWorld", "Hello World "};

        for (String s : testStrings) {
            Matcher matcher = pattern.matcher(s);
            if (matcher.find()) {
                System.out.println("The string \"" + s + "\" contains a blank character.");
            } else {
                System.out.println("The string \"" + s + "\" does not contain a blank character.");
            }
        }
    }


    /**
     *   ron@ronsoft.com,frd.@abc.com
     *   166954219@qq.com
     *
     *   使用这个参数
     * @param args
     */
    private static void replaceAll(String[] args) {
        if (args.length < 1) {
            System.out.println("请使用邮箱地址");
        }

        // 打开多行模式
        Pattern compile = Pattern.compile("([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]" +
                "{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))" +
                "([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)", Pattern.MULTILINE);

        Matcher matcher = compile.matcher("");
        CharSequence charSequence;

        // 便利参数并寻找地址
        for (int i = 0; i < args.length; i++) {
            boolean matched = false;
            System.out.println("");
            System.out.println("看这个参数" + args[i] + ".....");
            // 将匹配器复位，查看当前实参字符串
            matcher.reset(args[i]);
            while (matcher.find()) {
//                System.out.println(matcher.start() + "开始位置");
//                System.out.println(matcher.end() + "结束位置");

//                charSequence = args[i].subSequence(matcher.start(), matcher.end());
                charSequence = args[i].subSequence(matcher.start(2), matcher.end(2));
                System.out.println("\t" + charSequence.toString());

                // 找到一个匹配 和 charSequence = args[i].subSequence(matcher.start(), matcher.end());  一样的效果
                System.out.println("\t" + matcher.group());
                matched = true;
            }
            System.out.println(matcher.groupCount() + "捕获组的数量");
            if (!matched) {
                System.out.println("\t没有找到匹配");
            }
        }
    }


    /**
     * 正则表达式替换
     *
     * 参数  ([bB])yte
     * $1lite 必须写1
     * Byteisbytes
     */
    private static void replace(String [] args) {
        if(args.length < 3) {
            System.out.println("请输入三个以上的参数");
            return;
        }
        // 用助词符号名保存曾泽以及替换字符串
        String regex = args[0];
        System.out.println("正则表达式：" + regex);
        String replace = args[1];

        /**
         * 注意： 在编译时正则表达式不一定正确 会抛出异常（PatternSyntaxException）
         */
        // 编译表达式 一次只能编译一个
        Pattern compile = null;
        try {
            compile = Pattern.compile(regex);
            // 获取匹配器  暂时使用虚设的输入字符串
            Matcher matcher=  compile.matcher("");
            System.out.println("regex: '" + regex + "'");
            System.out.println("replacement : '" + replace + "'");

            // 对剩余的参数字符串使用正则/替换
            for (int i = 2; i < args.length; i++) {
                System.out.println("--------------");
                matcher.reset(args[i]);

                System.out.println("input :'" + args[i] + "'");
                System.out.println("matchGroup : '" + matcher.groupCount()+ "'");
//                System.out.println("replaceFirst : '" + matcher.replaceFirst(replace) + "'");
                System.out.println("replaceAll: '" + matcher.replaceAll(replace) + "'");
            }
        } catch (Exception e) {
//            Logging.valueOf("regex expression ："+ regex + " is not illegal");
            e.printStackTrace();
        }


    }
}
